import 'package:flutter/material.dart';

typedef ValueChange = void Function(TextEditingController controller, FocusNode focusNode, String value);

class InputField extends StatefulWidget {
  final String initialValue;
  final String hintText;
  final TextStyle? textStyle;
  final TextStyle? hintStyle;
  final Widget? prefix;
  final Widget? suffix;
  final int? maxLines;
  final int? maxLength;
  final Color? cursorColor;
  final TextInputType keyboardType;
  final TextInputAction textInputAction;
  final TextAlign textAlign;
  final EdgeInsets contentPadding;
  final bool autofocus;
  final bool disabled;
  final TextEditingController? controller;
  final FocusNode? focusNode;
  final ValueChange? onChanged;
  final ValueChange? onSubmitted;
  final ValueChange? onFocus;
  final ValueChange? onBlur;

  const InputField({
    super.key,
    this.initialValue = '',
    this.hintText = '请输入',
    this.textStyle,
    this.hintStyle,
    this.prefix,
    this.suffix,
    this.maxLines = 1,
    this.maxLength,
    this.cursorColor = Colors.white,
    this.keyboardType = TextInputType.text,
    this.textInputAction = TextInputAction.done,
    this.textAlign = TextAlign.left,
    this.contentPadding = EdgeInsets.zero,
    this.autofocus = false,
    this.disabled = false,
    this.controller,
    this.focusNode,
    this.onChanged,
    this.onSubmitted,
    this.onFocus,
    this.onBlur,
  });

  @override
  State<InputField> createState() => _InputFieldState();
}

class _InputFieldState extends State<InputField> {
  TextEditingController? _controller;
  FocusNode? _focusNode;
  bool _submit = false;

  @override
  void initState() {
    _focusNode = widget.focusNode ?? FocusNode();
    _controller = widget.controller ?? TextEditingController(text: widget.initialValue);
    _controller!.text = widget.initialValue;
    _focusNode!.addListener(() {
      if (!_submit) {
        if (_focusNode!.hasFocus) {
          widget.onFocus?.call(_controller!, _focusNode!, _controller!.value.text.trim());
        } else {
          widget.onBlur?.call(_controller!, _focusNode!, _controller!.value.text.trim());
        }
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    if (widget.controller == null) _controller?.dispose();
    if (widget.focusNode == null) _focusNode?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => TextField(
        focusNode: _focusNode,
        controller: _controller,
        maxLines: widget.maxLines,
        maxLength: widget.maxLength,
        cursorColor: widget.cursorColor,
        keyboardType: widget.keyboardType,
        textInputAction: widget.textInputAction,
        textAlignVertical: TextAlignVertical.center,
        textAlign: widget.textAlign,
        style: widget.textStyle,
        autofocus: widget.autofocus,
        readOnly: widget.disabled,
        decoration: InputDecoration(
          counterText: '',
          hintText: widget.hintText,
          hintStyle: widget.hintStyle,
          prefix: widget.prefix,
          suffix: widget.suffix,
          contentPadding: widget.contentPadding,
          constraints: const BoxConstraints(),
        ),
        onChanged: (value) => widget.onChanged?.call(_controller!, _focusNode!, value.trim()),
        onSubmitted: (value) {
          _submit = true;
          widget.onSubmitted?.call(_controller!, _focusNode!, value.trim());
        },
      );
}
